home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / hexl.el < prev    next >
Encoding:
Text File  |  1995-03-25  |  24.4 KB  |  690 lines

  1. ;;; hexl.el --- edit a file in a hex dump format using the hexl filter.
  2.  
  3. ;; Copyright (C) 1989, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Keith Gabryelski <ag@wheaties.ai.mit.edu>
  6. ;; Keywords: data
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Synched up with: Not synched with FSF.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This package implements a major mode for editing binary files.  It uses
  29. ;; a program called hexl, supplied with the GNU Emacs distribution, that
  30. ;; can filter a binary into an editable format or from the format back into
  31. ;; binary.  For full instructions, invoke `hexl-mode' on an empty buffer and
  32. ;; do `M-x describe-mode'.
  33. ;;
  34. ;; This may be useful in your .emacs:
  35. ;;
  36. ;;    (autoload 'hexl-find-file "hexl"
  37. ;;      "Edit file FILENAME in hexl-mode." t)
  38. ;;    
  39. ;;    (define-key global-map "\C-c\C-h" 'hexl-find-file)
  40. ;;
  41. ;; NOTE: Remember to change HEXL-PROGRAM or HEXL-OPTIONS if needed.
  42. ;;
  43. ;; Currently hexl only supports big endian hex output with 16 bit
  44. ;; grouping.
  45. ;;
  46. ;; -iso in `hexl-options' will allow iso characters to display in the
  47. ;; ASCII region of the screen (if your emacs supports this) instead of
  48. ;; changing them to dots.
  49.  
  50. ;;; Code:
  51.  
  52. ;;
  53. ;; vars here
  54. ;;
  55.  
  56. (defvar hexl-program "hexl"
  57.   "The program that will hexlify and de-hexlify its stdin.
  58. `hexl-program' will always be concatenated with `hexl-options'
  59. and \"-de\" when dehexlfying a buffer.")
  60.  
  61. (defvar hexl-iso ""
  62.   "If your emacs can handle ISO characters, this should be set to
  63. \"-iso\" otherwise it should be \"\".")
  64.  
  65. (defvar hexl-options (format "-hex %s" hexl-iso)
  66.   "Options to hexl-program that suit your needs.")
  67.  
  68. (defvar hexlify-command
  69.   (format "%s%s %s" exec-directory hexl-program hexl-options)
  70.   "The command to use to hexlify a buffer.")
  71.  
  72. (defvar dehexlify-command
  73.   (format "%s%s -de %s" exec-directory hexl-program hexl-options)
  74.   "The command to use to unhexlify a buffer.")
  75.  
  76. (defvar hexl-max-address 0
  77.   "Maximum offset into hexl buffer.")
  78.  
  79. (defvar hexl-mode-map nil)
  80.  
  81. ;; routines
  82.  
  83. (defvar hexl-mode-old-local-map)
  84. (defvar hexl-mode-old-mode-name)
  85. (defvar hexl-mode-old-major-mode)
  86.  
  87. ;;;###autoload
  88. (defun hexl-mode (&optional arg)
  89.   "\\<hexl-mode-map>
  90. A major mode for editing binary files in hex dump format.
  91.  
  92. This function automatically converts a buffer into the hexl format
  93. using the function `hexlify-buffer'.
  94.  
  95. Each line in the buffer has an \"address\" (displayed in hexadecimal)
  96. representing the offset into the file that the characters on this line
  97. are at and 16 characters from the file (displayed as hexadecimal
  98. values grouped every 16 bits) and as their ASCII values.
  99.  
  100. If any of the characters (displayed as ASCII characters) are
  101. unprintable (control or meta characters) they will be replaced as
  102. periods.
  103.  
  104. If `hexl-mode' is invoked with an argument the buffer is assumed to be
  105. in hexl format.
  106.  
  107. A sample format:
  108.  
  109.   HEX ADDR: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f     ASCII-TEXT
  110.   --------  ---- ---- ---- ---- ---- ---- ---- ----  ----------------
  111.   00000000: 5468 6973 2069 7320 6865 786c 2d6d 6f64  This is hexl-mod
  112.   00000010: 652e 2020 4561 6368 206c 696e 6520 7265  e.  Each line re
  113.   00000020: 7072 6573 656e 7473 2031 3620 6279 7465  presents 16 byte
  114.   00000030: 7320 6173 2068 6578 6164 6563 696d 616c  s as hexadecimal
  115.   00000040: 2041 5343 4949 0a61 6e64 2070 7269 6e74   ASCII.and print
  116.   00000050: 6162 6c65 2041 5343 4949 2063 6861 7261  able ASCII chara
  117.   00000060: 6374 6572 732e 2020 416e 7920 636f 6e74  cters.  Any cont
  118.   00000070: 726f 6c20 6f72 206e 6f6e 2d41 5343 4949  rol or non-ASCII
  119.   00000080: 2063 6861 7261 6374 6572 730a 6172 6520   characters.are 
  120.   00000090: 6469 7370 6c61 7965 6420 6173 2070 6572  displayed as per
  121.   000000a0: 696f 6473 2069 6e20 7468 6520 7072 696e  iods in the prin
  122.   000000b0: 7461 626c 6520 6368 6172 6163 7465 7220  table character 
  123.   000000c0: 7265 6769 6f6e 2e0a                      region..
  124.  
  125. Movement is as simple as movement in a normal emacs text buffer.  Most
  126. cursor movement bindings are the same (ie. Use \\[hexl-backward-char], \\[hexl-forward-char], \\[hexl-next-line], and \\[hexl-previous-line]
  127. to move the cursor left, right, down, and up).
  128.  
  129. Advanced cursor movement commands (ala \\[hexl-beginning-of-line], \\[hexl-end-of-line], \\[hexl-beginning-of-buffer], and \\[hexl-end-of-buffer]) are
  130. also supported.
  131.  
  132. There are several ways to change text in hexl mode:
  133.  
  134. ASCII characters (character between space (0x20) and tilde (0x7E)) are
  135. bound to self-insert so you can simply type the character and it will
  136. insert itself (actually overstrike) into the buffer.
  137.  
  138. \\[hexl-quoted-insert] followed by another keystroke allows you to insert the key even if
  139. it isn't bound to self-insert.  An octal number can be supplied in place
  140. of another key to insert the octal number's ASCII representation.
  141.  
  142. \\[hexl-insert-hex-char] will insert a given hexadecimal value (if it is between 0 and 0xFF)
  143. into the buffer at the current point.
  144.  
  145. \\[hexl-insert-octal-char] will insert a given octal value (if it is between 0 and 0377)
  146. into the buffer at the current point.
  147.  
  148. \\[hexl-insert-decimal-char] will insert a given decimal value (if it is between 0 and 255)
  149. into the buffer at the current point.
  150.  
  151. \\[hexl-mode-exit] will exit hexl-mode.
  152.  
  153. Note: saving the file with any of the usual Emacs commands
  154. will actually convert it back to binary format while saving.
  155.  
  156. You can use \\[hexl-find-file] to visit a file in hexl-mode.
  157.  
  158. \\[describe-bindings] for advanced commands."
  159.   (interactive "p")
  160.   (if (eq major-mode 'hexl-mode)
  161.       (error "You are already in hexl mode.")
  162.     (kill-all-local-variables)
  163.     (make-local-variable 'hexl-mode-old-local-map)
  164.     (setq hexl-mode-old-local-map (current-local-map))
  165.     (use-local-map hexl-mode-map)
  166.  
  167.     (make-local-variable 'hexl-mode-old-mode-name)
  168.     (setq hexl-mode-old-mode-name mode-name)
  169.     (setq mode-name "Hexl")
  170.  
  171.     (make-local-variable 'hexl-mode-old-major-mode)
  172.     (setq hexl-mode-old-major-mode major-mode)
  173.     (setq major-mode 'hexl-mode)
  174.  
  175.     (make-local-variable 'write-contents-hooks)
  176.     (add-hook 'write-contents-hooks 'hexl-save-buffer)
  177.  
  178.     (let ((modified (buffer-modified-p))
  179.        (read-only buffer-read-only)
  180.       (original-point (1- (point))))
  181.       (if (not (or (eq arg 1) (not arg)))
  182. ;; if no argument then we guess at hexl-max-address
  183.           (setq hexl-max-address (+ (* (/ (1- (buffer-size)) 68) 16) 15))
  184.         (setq buffer-read-only nil)
  185.         (setq hexl-max-address (1- (buffer-size)))
  186.         (hexlify-buffer)
  187.         (set-buffer-modified-p modified)
  188.         (setq buffer-read-only read-only)
  189.         (hexl-goto-address original-point)))))
  190.  
  191. (defvar hexl-in-save-buffer nil)
  192.  
  193. (defun hexl-save-buffer ()
  194.   "Save a hexl format buffer as binary in visited file if modified."
  195.   (interactive)
  196.   (if hexl-in-save-buffer nil
  197.     (set-buffer-modified-p (if (buffer-modified-p)
  198.                    (save-excursion
  199.                  (let ((buf (generate-new-buffer " hexl"))
  200.                        (name (buffer-name))
  201.                        (file-name (buffer-file-name))
  202.                        (start (point-min))
  203.                        (end (point-max))
  204.                        modified)
  205.                    (set-buffer buf)
  206.                    (insert-buffer-substring name start end)
  207.                    (set-buffer name)
  208.                    (dehexlify-buffer)
  209.                    ;; Prevent infinite recursion.
  210.                    (let ((hexl-in-save-buffer t))
  211.                      (save-buffer))
  212.                    (setq modified (buffer-modified-p))
  213.                    (delete-region (point-min) (point-max))
  214.                    (insert-buffer-substring buf start end)
  215.                    (kill-buffer buf)
  216.                    modified))
  217.                  (message "(No changes need to be saved)")
  218.                  nil))
  219.     ;; Return t to indicate we have saved t
  220.     t))
  221.  
  222. ;;;###autoload
  223. (defun hexl-find-file (filename)
  224.   "Edit file FILENAME in hexl-mode.
  225. Switch to a buffer visiting file FILENAME, creating one in none exists."
  226.   (interactive "fFilename: ")
  227.   (find-file filename)
  228.   (if (not (eq major-mode 'hexl-mode))
  229.       (hexl-mode)))
  230.  
  231. (defun hexl-mode-exit (&optional arg)
  232.   "Exit Hexl mode, returning to previous mode.
  233. With arg, don't unhexlify buffer."
  234.   (interactive "p")
  235.   (if (or (eq arg 1) (not arg))
  236.       (let ((modified (buffer-modified-p))
  237.         (read-only buffer-read-only)
  238.         (original-point (1+ (hexl-current-address))))
  239.     (setq buffer-read-only nil)
  240.     (dehexlify-buffer)
  241.     (remove-hook 'write-contents-hook 'hexl-save-buffer)
  242.     (set-buffer-modified-p modified)
  243.     (setq buffer-read-only read-only)
  244.     (goto-char original-point)))
  245.   (setq mode-name hexl-mode-old-mode-name)
  246.   (use-local-map hexl-mode-old-local-map)
  247.   (setq major-mode hexl-mode-old-major-mode)
  248. ;; Kludge to update mode-line
  249.   (switch-to-buffer (current-buffer))
  250. )
  251.  
  252. (defun hexl-current-address ()
  253.   "Return current hexl-address."
  254.   (interactive)
  255.   (let ((current-column (- (% (point) 68) 11)) 
  256.     (hexl-address 0))
  257.     (setq hexl-address (+ (* (/ (point) 68) 16)
  258.               (/ (- current-column  (/ current-column 5)) 2)))
  259.     hexl-address))
  260.  
  261. (defun hexl-address-to-marker (address)
  262.   "Return marker for ADDRESS."
  263.   (interactive "nAddress: ")
  264.   (+ (* (/ address 16) 68) 11 (/ (* (% address 16) 5) 2)))
  265.  
  266. (defun hexl-goto-address (address)
  267.   "Goto hexl-mode (decimal) address ADDRESS.
  268.  
  269. Signal error if ADDRESS out of range."
  270.   (interactive "nAddress: ")
  271.   (if (or (< address 0) (> address hexl-max-address))
  272.       (error "Out of hexl region."))
  273.   (goto-char (hexl-address-to-marker address)))
  274.  
  275. (defun hexl-goto-hex-address (hex-address)
  276.   "Go to hexl-mode address (hex string) HEX-ADDRESS.
  277.  
  278. Signal error if HEX-ADDRESS is out of range."
  279.   (interactive "sHex Address: ")
  280.   (hexl-goto-address (hexl-hex-string-to-integer hex-address)))
  281.  
  282. (defun hexl-hex-string-to-integer (hex-string)
  283.   "Return decimal integer for HEX-STRING."
  284.   (interactive "sHex number: ")
  285.   (let ((hex-num 0))
  286.     (while (not (equal hex-string ""))
  287.       (setq hex-num (+ (* hex-num 16)
  288.                (hexl-hex-char-to-integer (string-to-char hex-string))))
  289.       (setq hex-string (substring hex-string 1)))
  290.     hex-num))
  291.  
  292. (defun hexl-octal-string-to-integer (octal-string)
  293.   "Return decimal integer for OCTAL-STRING."
  294.   (interactive "sOctal number: ")
  295.   (let ((oct-num 0))
  296.     (while (not (equal octal-string ""))
  297.       (setq oct-num (+ (* oct-num 8)
  298.                (hexl-oct-char-to-integer
  299.             (string-to-char octal-string))))
  300.       (setq octal-string (substring octal-string 1)))
  301.     oct-num))
  302.  
  303. ;; move point functions
  304.  
  305. (defun hexl-backward-char (arg)
  306.   "Move to left ARG bytes (right if ARG negative) in hexl-mode."
  307.   (interactive "p")
  308.   (hexl-goto-address (- (hexl-current-address) arg)))
  309.  
  310. (defun hexl-forward-char (arg)
  311.   "Move right ARG bytes (left if ARG negative) in hexl-mode."
  312.   (interactive "p")
  313.   (hexl-goto-address (+ (hexl-current-address) arg)))
  314.  
  315. (defun hexl-backward-short (arg)
  316.   "Move to left ARG shorts (right if ARG negative) in hexl-mode."
  317.   (interactive "p")
  318.   (hexl-goto-address (let ((address (hexl-current-address)))
  319.                (if (< arg 0)
  320.                (progn
  321.                  (setq arg (- arg))
  322.                  (while (> arg 0)
  323.                    (if (not (equal address (logior address 3)))
  324.                    (if (> address hexl-max-address)
  325.                        (progn
  326.                      (message "End of buffer.")
  327.                      (setq address hexl-max-address))
  328.                      (setq address (logior address 3)))
  329.                  (if (> address hexl-max-address)
  330.                      (progn
  331.                        (message "End of buffer.")
  332.                        (setq address hexl-max-address))
  333.                    (setq address (+ address 4))))
  334.                    (setq arg (1- arg)))
  335.                  (if (> address hexl-max-address)
  336.                  (progn
  337.                    (message "End of buffer.")
  338.                    (setq address hexl-max-address))
  339.                    (setq address (logior address 3))))
  340.              (while (> arg 0)
  341.                (if (not (equal address (logand address -4)))
  342.                    (setq address (logand address -4))
  343.                  (if (not (equal address 0))
  344.                  (setq address (- address 4))
  345.                    (message "Beginning of buffer.")))
  346.                (setq arg (1- arg))))
  347.                address)))
  348.  
  349. (defun hexl-forward-short (arg)
  350.   "Move right ARG shorts (left if ARG negative) in hexl-mode."
  351.   (interactive "p")
  352.   (hexl-backward-short (- arg)))
  353.  
  354. (defun hexl-backward-word (arg)
  355.   "Move to left ARG words (right if ARG negative) in hexl-mode."
  356.   (interactive "p")
  357.   (hexl-goto-address (let ((address (hexl-current-address)))
  358.                (if (< arg 0)
  359.                (progn
  360.                  (setq arg (- arg))
  361.                  (while (> arg 0)
  362.                    (if (not (equal address (logior address 7)))
  363.                    (if (> address hexl-max-address)
  364.                        (progn
  365.                      (message "End of buffer.")
  366.                      (setq address hexl-max-address))
  367.                      (setq address (logior address 7)))
  368.                  (if (> address hexl-max-address)
  369.                      (progn
  370.                        (message "End of buffer.")
  371.                        (setq address hexl-max-address))
  372.                    (setq address (+ address 8))))
  373.                    (setq arg (1- arg)))
  374.                  (if (> address hexl-max-address)
  375.                  (progn
  376.                    (message "End of buffer.")
  377.                    (setq address hexl-max-address))
  378.                    (setq address (logior address 7))))
  379.              (while (> arg 0)
  380.                (if (not (equal address (logand address -8)))
  381.                    (setq address (logand address -8))
  382.                  (if (not (equal address 0))
  383.                  (setq address (- address 8))
  384.                    (message "Beginning of buffer.")))
  385.                (setq arg (1- arg))))
  386.                address)))
  387.  
  388. (defun hexl-forward-word (arg)
  389.   "Move right ARG words (left if ARG negative) in hexl-mode."
  390.   (interactive "p")
  391.   (hexl-backward-word (- arg)))
  392.  
  393. (defun hexl-previous-line (arg)
  394.   "Move vertically up ARG lines [16 bytes] (down if ARG negative) in hexl-mode.
  395. If there is byte at the target address move to the last byte in that line."
  396.   (interactive "p")
  397.   (hexl-next-line (- arg)))
  398.  
  399. (defun hexl-next-line (arg)
  400.   "Move vertically down ARG lines [16 bytes] (up if ARG negative) in hexl-mode.
  401. If there is no byte at the target address move to the last byte in that line."
  402.   (interactive "p")
  403.   (hexl-goto-address (let ((address (+ (hexl-current-address) (* arg 16))))
  404.                (if (and (< arg 0) (< address 0))
  405.                 (progn (message "Out of hexl region.")
  406.                        (setq address
  407.                          (% (hexl-current-address) 16)))
  408.              (if (and (> address hexl-max-address)
  409.                   (< (% hexl-max-address 16) (% address 16)))
  410.                  (setq address hexl-max-address)
  411.                (if (> address hexl-max-address)
  412.                    (progn (message "Out of hexl region.")
  413.                       (setq
  414.                        address
  415.                        (+ (logand hexl-max-address -16)
  416.                       (% (hexl-current-address) 16)))))))
  417.                address)))
  418.  
  419. (defun hexl-beginning-of-buffer (arg)
  420.   "Move to the beginning of the hexl buffer.
  421. Leaves `hexl-mark' at previous position.
  422. With prefix arg N, puts point N bytes of the way from the true beginning."
  423.   (interactive "p")
  424.   (push-mark (point))
  425.   (hexl-goto-address (+ 0 (1- arg))))
  426.  
  427. (defun hexl-end-of-buffer (arg)
  428.   "Go to `hexl-max-address' minus ARG."
  429.   (interactive "p")
  430.   (push-mark (point))
  431.   (hexl-goto-address (- hexl-max-address (1- arg))))
  432.  
  433. (defun hexl-beginning-of-line ()
  434.   "Goto beginning of line in hexl mode."
  435.   (interactive)
  436.   (goto-char (+ (* (/ (point) 68) 68) 11)))
  437.  
  438. (defun hexl-end-of-line ()
  439.   "Goto end of line in hexl mode."
  440.   (interactive)
  441.   (hexl-goto-address (let ((address (logior (hexl-current-address) 15)))
  442.                (if (> address hexl-max-address)
  443.                (setq address hexl-max-address))
  444.                address)))
  445.  
  446. (defun hexl-scroll-down (arg)
  447.   "Scroll hexl buffer window upward ARG lines; or near full window if no ARG."
  448.   (interactive "P")
  449.   (if (null arg)
  450.       (setq arg (1- (window-height)))
  451.     (setq arg (prefix-numeric-value arg)))
  452.   (hexl-scroll-up (- arg)))
  453.  
  454. (defun hexl-scroll-up (arg)
  455.   "Scroll hexl buffer window upward ARG lines; or near full window if no ARG."
  456.   (interactive "P")
  457.   (if (null arg)
  458.       (setq arg (1- (window-height)))
  459.     (setq arg (prefix-numeric-value arg)))
  460.   (let ((movement (* arg 16))
  461.     (address (hexl-current-address)))
  462.     (if (or (> (+ address movement) hexl-max-address)
  463.         (< (+ address movement) 0))
  464.     (message "Out of hexl region.")
  465.       (hexl-goto-address (+ address movement))
  466.       (recenter 0))))
  467.  
  468. (defun hexl-beginning-of-1k-page ()
  469.   "Goto to beginning of 1k boundry."
  470.   (interactive)
  471.   (hexl-goto-address (logand (hexl-current-address) -1024)))
  472.  
  473. (defun hexl-end-of-1k-page ()
  474.   "Goto to end of 1k boundry."
  475.   (interactive)
  476.   (hexl-goto-address (let ((address (logior (hexl-current-address) 1023)))
  477.                (if (> address hexl-max-address)
  478.                (setq address hexl-max-address))
  479.                address)))
  480.  
  481. (defun hexl-beginning-of-512b-page ()
  482.   "Goto to beginning of 512 byte boundry."
  483.   (interactive)
  484.   (hexl-goto-address (logand (hexl-current-address) -512)))
  485.  
  486. (defun hexl-end-of-512b-page ()
  487.   "Goto to end of 512 byte boundry."
  488.   (interactive)
  489.   (hexl-goto-address (let ((address (logior (hexl-current-address) 511)))
  490.                (if (> address hexl-max-address)
  491.                (setq address hexl-max-address))
  492.                address)))
  493.  
  494. (defun hexl-quoted-insert (arg)
  495.   "Read next input character and insert it.
  496. Useful for inserting control characters.
  497. You may also type up to 3 octal digits, to insert a character with that code"
  498.   (interactive "p")
  499.   (hexl-insert-char (read-quoted-char) arg))
  500.  
  501. ;00000000: 0011 2233 4455 6677 8899 aabb ccdd eeff  0123456789ABCDEF
  502.  
  503. (defun hexlify-buffer ()
  504.   "Convert a binary buffer to hexl format"
  505.   (interactive)
  506.   (shell-command-on-region (point-min) (point-max) hexlify-command t))
  507.  
  508. (defun dehexlify-buffer ()
  509.   "Convert a hexl format buffer to binary."
  510.   (interactive)
  511.   (shell-command-on-region (point-min) (point-max) dehexlify-command t))
  512.  
  513. (defun hexl-char-after-point ()
  514.   "Return char for ASCII hex digits at point."
  515.   (hexl-htoi (char-after (point))
  516.          (char-after (1+ (point)))))
  517.  
  518. (defun hexl-htoi (lh rh)
  519.   "Hex (char) LH (char) RH to integer."
  520.     (+ (* (hexl-hex-char-to-integer lh) 16)
  521.        (hexl-hex-char-to-integer rh)))
  522.  
  523. (defun hexl-hex-char-to-integer (character)
  524.   "Take a char and return its value as if it was a hex digit."
  525.   (if (and (>= character ?0) (<= character ?9))
  526.       (- character ?0)
  527.     (let ((ch (logior character 32)))
  528.       (if (and (>= ch ?a) (<= ch ?f))
  529.       (- ch (- ?a 10))
  530.     (error (format "Invalid hex digit `%c'." ch))))))
  531.  
  532. (defun hexl-oct-char-to-integer (character)
  533.   "Take a char and return its value as if it was a octal digit."
  534.   (if (and (>= character ?0) (<= character ?7))
  535.       (- character ?0)
  536.     (error (format "Invalid octal digit `%c'." character))))
  537.  
  538. (defun hexl-printable-character (ch)
  539.   "Return a displayable string for character CH."
  540.   (format "%c" (if hexl-iso
  541.            (if (or (< ch 32) (and (>= ch 127) (< ch 160)))
  542.                46
  543.              ch)
  544.          (if (or (< ch 32) (>= ch 127))
  545.              46
  546.            ch))))
  547.  
  548. (defun hexl-self-insert-command (arg)
  549.   "Insert this character."
  550.   (interactive "p")
  551.   (hexl-insert-char last-command-char arg))
  552.  
  553. (defun hexl-insert-char (ch num)
  554.   "Insert a character in a hexl buffer."
  555.   (let ((address (hexl-current-address)))
  556.     (while (> num 0)
  557.       (delete-char 2)
  558.       (insert (format "%02x" ch))
  559.       (goto-char
  560.        (+ (* (/ address 16) 68) 52 (% address 16)))
  561.       (delete-char 1)
  562.       (insert (hexl-printable-character ch))
  563.       (if (eq address hexl-max-address)
  564.       (hexl-goto-address address)
  565.     (hexl-goto-address (1+ address)))
  566.       (setq num (1- num)))))
  567.  
  568. ;; hex conversion
  569.  
  570. (defun hexl-insert-hex-char (arg)
  571.   "Insert a ASCII char ARG times at point for a given hexadecimal number."
  572.   (interactive "p")
  573.   (let ((num (hexl-hex-string-to-integer (read-string "Hex number: "))))
  574.     (if (or (> num 255) (< num 0))
  575.     (error "Hex number out of range.")
  576.       (hexl-insert-char num arg))))
  577.  
  578. (defun hexl-insert-decimal-char (arg)
  579.   "Insert a ASCII char ARG times at point for a given decimal number."
  580.   (interactive "p")
  581.   (let ((num (string-to-int (read-string "Decimal Number: "))))
  582.     (if (or (> num 255) (< num 0))
  583.     (error "Decimal number out of range.")
  584.       (hexl-insert-char num arg))))
  585.  
  586. (defun hexl-insert-octal-char (arg)
  587.   "Insert a ASCII char ARG times at point for a given octal number."
  588.   (interactive "p")
  589.   (let ((num (hexl-octal-string-to-integer (read-string "Octal Number: "))))
  590.     (if (or (> num 255) (< num 0))
  591.     (error "Decimal number out of range.")
  592.       (hexl-insert-char num arg))))
  593.  
  594. ;; startup stuff.
  595.  
  596. (if hexl-mode-map
  597.     nil
  598.     (setq hexl-mode-map (make-sparse-keymap))
  599.     (set-keymap-name hexl-mode-map 'hexl-mode-map)
  600.  
  601.     (define-key hexl-mode-map "\C-a" 'hexl-beginning-of-line)
  602.     (define-key hexl-mode-map "\C-b" 'hexl-backward-char)
  603.     (define-key hexl-mode-map "\C-d" 'undefined)
  604.     (define-key hexl-mode-map "\C-e" 'hexl-end-of-line)
  605.     (define-key hexl-mode-map "\C-f" 'hexl-forward-char)
  606.  
  607.     (if (not (eq (key-binding (char-to-string help-char)) 'help-command))
  608.     (define-key hexl-mode-map (char-to-string help-char) 'undefined))
  609.  
  610.     (define-key hexl-mode-map "\C-i" 'hexl-self-insert-command)
  611.     (define-key hexl-mode-map "\C-j" 'hexl-self-insert-command)
  612.     (define-key hexl-mode-map "\C-k" 'undefined)
  613.     (define-key hexl-mode-map "\C-m" 'hexl-self-insert-command)
  614.     (define-key hexl-mode-map "\C-n" 'hexl-next-line)
  615.     (define-key hexl-mode-map "\C-o" 'undefined)
  616.     (define-key hexl-mode-map "\C-p" 'hexl-previous-line)
  617.     (define-key hexl-mode-map "\C-q" 'hexl-quoted-insert)
  618.     (define-key hexl-mode-map "\C-t" 'undefined)
  619.     (define-key hexl-mode-map "\C-v" 'hexl-scroll-up)
  620.     (define-key hexl-mode-map "\C-w" 'undefined)
  621.     (define-key hexl-mode-map "\C-y" 'undefined)
  622.  
  623.     (let ((ch 32))
  624.       (while (< ch 127)
  625.     (define-key hexl-mode-map (char-to-string ch) 'hexl-self-insert-command)
  626.     (setq ch (1+ ch))))
  627.  
  628.     (define-key hexl-mode-map "\e\C-a" 'hexl-beginning-of-512b-page)
  629.     (define-key hexl-mode-map "\e\C-b" 'hexl-backward-short)
  630.     (define-key hexl-mode-map "\e\C-c" 'undefined)
  631.     (define-key hexl-mode-map "\e\C-d" 'hexl-insert-decimal-char)
  632.     (define-key hexl-mode-map "\e\C-e" 'hexl-end-of-512b-page)
  633.     (define-key hexl-mode-map "\e\C-f" 'hexl-forward-short)
  634.     (define-key hexl-mode-map "\e\C-g" 'undefined)
  635.     (define-key hexl-mode-map "\e\C-h" 'undefined)
  636.     (define-key hexl-mode-map "\e\C-i" 'undefined)
  637.     (define-key hexl-mode-map "\e\C-j" 'undefined)
  638.     (define-key hexl-mode-map "\e\C-k" 'undefined)
  639.     (define-key hexl-mode-map "\e\C-l" 'undefined)
  640.     (define-key hexl-mode-map "\e\C-m" 'undefined)
  641.     (define-key hexl-mode-map "\e\C-n" 'undefined)
  642.     (define-key hexl-mode-map "\e\C-o" 'hexl-insert-octal-char)
  643.     (define-key hexl-mode-map "\e\C-p" 'undefined)
  644.     (define-key hexl-mode-map "\e\C-q" 'undefined)
  645.     (define-key hexl-mode-map "\e\C-r" 'undefined)
  646.     (define-key hexl-mode-map "\e\C-s" 'undefined)
  647.     (define-key hexl-mode-map "\e\C-t" 'undefined)
  648.     (define-key hexl-mode-map "\e\C-u" 'undefined)
  649.  
  650.     (define-key hexl-mode-map "\e\C-w" 'undefined)
  651.     (define-key hexl-mode-map "\e\C-x" 'hexl-insert-hex-char)
  652.     (define-key hexl-mode-map "\e\C-y" 'undefined)
  653.  
  654.     (define-key hexl-mode-map "\ea" 'undefined) ;hexl-beginning-of-1k-page
  655.     (define-key hexl-mode-map "\eb" 'hexl-backward-word)
  656.     (define-key hexl-mode-map "\ec" 'undefined)
  657.     (define-key hexl-mode-map "\ed" 'undefined)
  658.     (define-key hexl-mode-map "\ee" 'undefined) ;hexl-end-of-1k-page
  659.     (define-key hexl-mode-map "\ef" 'hexl-forward-word)
  660.     (define-key hexl-mode-map "\eg" 'hexl-goto-hex-address)
  661.     (define-key hexl-mode-map "\eh" 'undefined)
  662.     (define-key hexl-mode-map "\ei" 'undefined)
  663.     (define-key hexl-mode-map "\ej" 'hexl-goto-address)
  664.     (define-key hexl-mode-map "\ek" 'undefined)
  665.     (define-key hexl-mode-map "\el" 'undefined)
  666.     (define-key hexl-mode-map "\em" 'undefined)
  667.     (define-key hexl-mode-map "\en" 'undefined)
  668.     (define-key hexl-mode-map "\eo" 'undefined)
  669.     (define-key hexl-mode-map "\ep" 'undefined)
  670.     (define-key hexl-mode-map "\eq" 'undefined)
  671.     (define-key hexl-mode-map "\er" 'undefined)
  672.     (define-key hexl-mode-map "\es" 'undefined)
  673.     (define-key hexl-mode-map "\et" 'undefined)
  674.     (define-key hexl-mode-map "\eu" 'undefined)
  675.     (define-key hexl-mode-map "\ev" 'hexl-scroll-down)
  676.     (define-key hexl-mode-map "\ey" 'undefined)
  677.     (define-key hexl-mode-map "\ez" 'undefined)
  678.     (define-key hexl-mode-map "\e<" 'hexl-beginning-of-buffer)
  679.     (define-key hexl-mode-map "\e>" 'hexl-end-of-buffer)
  680.  
  681.     (define-key hexl-mode-map "\C-c\C-c" 'hexl-mode-exit)
  682.  
  683.     (define-key hexl-mode-map "\C-x[" 'hexl-beginning-of-1k-page)
  684.     (define-key hexl-mode-map "\C-x]" 'hexl-end-of-1k-page)
  685.     (define-key hexl-mode-map "\C-x\C-p" 'undefined)
  686.     (define-key hexl-mode-map "\C-x\C-s" 'hexl-save-buffer)
  687.     (define-key hexl-mode-map "\C-x\C-t" 'undefined))
  688.  
  689. ;;; hexl.el ends here
  690.